
// load the active users results
function loadMyResults() {
	
	new Ajax.Request( siteUrl + 'modules.php?action=loadMyResults', {
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				$('centerArea').update( response.responseText ).show();
				}
		},
		onFailure: function( response ) {
			$('centerArea').update( 'Unknown error.' ).show();
		}
	});
}

//load result for grading and giving feedback
function loadResult(resultId){
	var resultId = resultId;
	new Ajax.Request( siteUrl + 'results.php?action=loadResult&resultId='+resultId, {
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				$('centerArea').update( response.responseText ).show();
			}
		},
		onFailure: function( response ) {
			$('centerArea').update( 'Unknown error.' ).show();
		}
	});
}

//views a result (points feedback). For the student.
function viewResult(resultId){
	var resultId = resultId;
	new Ajax.Request( siteUrl + 'results.php?action=viewResult&resultId='+resultId, {
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				$('centerArea').update( response.responseText ).show();
			}
		},
		onFailure: function( response ) {
			$('centerArea').update( 'Unknown error.' ).show();
		}
	});
}

// load all results (for the teacher)
function loadAllResults(){
	new Ajax.Request( siteUrl + 'modules.php?action=loadAllResults', {
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				$('centerArea').update( response.responseText ).show();
			}
		},
		onFailure: function( response ) {
			$('centerArea').update( 'Unknown error.' ).show();
		}
	});
}

// load the results of the specified user
function loadUserResults(userId){
	var userId = userId;
	new Ajax.Request( siteUrl + 'modules.php?action=loadUserResults&userId='+userId, {
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				$('centerArea').update( response.responseText ).show();
			}
		},
		onFailure: function( response ) {
			$('centerArea').update( 'Unknown error.' ).show();
		}
	});
}

//load the results of the specified user
function resetFeedBackStatus(answerId){
	$('answerStatus'+answerId).update("").show();
}

//load the results of the specified user
function setFeedback(answerId){
	var answerId = answerId;
	var feedback = $('entity'+answerId).value;
	var points = $('points'+answerId).value;
	new Ajax.Request( siteUrl + 'results.php', {
		parameters: {action: 'setFeedback', answerId: answerId,
			points: points, feedback: feedback},
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				if(response.responseText =="OK"){
					alert("Salvestatud");
					$('answerStatus'+answerId).update("Tagasiside salvestatud").show();
				} else alert("Viga. Andmeid kas ei saanud salvestada või ei olnud muudatusi mida salvestada.");
				}
			},
		onFailure: function( response ) {
			alert("Viga. Proovi hiljem uuesti.");
		}
	});
}

//load the results of the specified user
function gradeResult(resultId){
	var resultId = resultId;
	new Ajax.Request( siteUrl + 'results.php', {
		parameters: {action: 'gradeResult', resultId: resultId},
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				if(response.responseText =="OK"){
					loadAllResults();
				} else alert("Viga. Andmeid kas ei saanud salvestada või ei olnud muudatusi mida salvestada.");
				}
		},
		onFailure: function( response ) {
			alert("Viga. Proovi hiljem uuesti.");
		}
	});
}

//end result (student finishes)
function endResult(resultId) {
	var decision = confirm ("Oled kindel et tahad lõpetada?");
	if(decision){
		new Ajax.Request( siteUrl + 'results.php', {
			parameters: {action: 'endResult', resultId: resultId},
			onSuccess: function( response ) {
				if(response.responseText == "LoggedOut"){
					resetPage();
				} else {
					loadOpenTests();
					}
			},
			onFailure: function( response ) {
				$('centerArea').update( 'Unknown error.' ).show();
			}
		});
	}
	
}
